using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
using Newtonsoft.Json;

namespace WindowsFormsApp1
{
    /// <summary>
    /// TCP客户端工具类
    /// 用于向WorkPieceDataCapture推送员工数据
    /// </summary>
    public class TcpClientHelper
    {
        private readonly string serverHost;
        private readonly int serverPort;
        private readonly int timeoutMs;

        public TcpClientHelper(string host = "localhost", int port = 8888, int timeout = 50000)
        {
            this.serverHost = host;
            this.serverPort = port;
            this.timeoutMs = timeout;
        }

        /// <summary>
        /// 发送员工数据到WorkPieceDataCapture
        /// </summary>
        /// <param name="employeeData">员工数据</param>
        /// <returns>是否发送成功</returns>
        public bool SendEmployeeData(object employeeData)
        {
            try
            {
                using (var client = new TcpClient())
                {
                    // 设置连接超时
                    client.ReceiveTimeout = timeoutMs;
                    client.SendTimeout = timeoutMs;
                    
                    client.Connect(serverHost, serverPort);

                    if (!client.Connected)
                    {
                        Console.WriteLine($"无法连接到TCP服务器: {serverHost}:{serverPort}");
                        return false;
                    }

                    using (var stream = client.GetStream())
                    using (var writer = new StreamWriter(stream, Encoding.UTF8))
                    using (var reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        // 序列化数据为JSON
                        var json = JsonConvert.SerializeObject(employeeData);
                        Console.WriteLine($"发送员工数据到WorkPieceDataCapture: {json}");

                        // 发送数据
                        writer.WriteLine(json);
                        writer.Flush();

                        // 读取服务器响应（使用ReadLine避免无限等待）
                        var response = reader.ReadLine();
                        Console.WriteLine($"服务器响应: {response}");

                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发送员工数据失败: {ex.Message}");
                return false;
            }
        }

        /// <summary>
        /// 测试连接是否可用
        /// </summary>
        /// <returns>连接是否成功</returns>
        public bool TestConnection()
        {
            try
            {
                using (var client = new TcpClient())
                {
                    client.ReceiveTimeout = timeoutMs;
                    client.SendTimeout = timeoutMs;
                    client.Connect(serverHost, serverPort);
                    return client.Connected;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"测试TCP连接失败: {ex.Message}");
                return false;
            }
        }
    }
}
